 package com.wangY.test.excelToJava.excel;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;















import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.wangY.test.excelToJava.entity.Common;
import com.wangY.test.excelToJava.entity.Pojo;



public class ReadExcel {

	public List<Pojo> readExcel(String path, int peg) throws IOException {
		if (path == null || Common.EMPTY.equals(path)) {
			return null;
		} else {
			if (peg == 0) {
				return readXls(path);
			}
			if (peg == 1) {
				return readXlsx(path);
			} else {
				System.out.println(path + Common.NOT_EXCEL_FILE);
			}
		}
		return null;
	}

	public List<Pojo> readXlsx(String path) throws IOException {
		System.out.println(Common.PROCESSING + path);

		InputStream input = new FileInputStream(path);
		XSSFWorkbook work = new XSSFWorkbook(input);
		Pojo pojo = null;
		List<Pojo> list = new ArrayList<Pojo>();
		for (int i = 0; i < work.getNumberOfSheets(); i++) {
			XSSFSheet sheet = work.getSheetAt(i);
			if (sheet == null) {
				continue;
			}
			for (int j = 0; j <= sheet.getLastRowNum(); j++) {
				XSSFRow row = sheet.getRow(j);
				if (row != null) {
					pojo = new Pojo();
					pojo.setA(getValue(row.getCell(0)));
					pojo.setB(getValue(row.getCell(1)));
					pojo.setC(getValue(row.getCell(2)));
					pojo.setD(getValue(row.getCell(3)));
					pojo.setE(getValue(row.getCell(4)));
					pojo.setF(getValue(row.getCell(5)));
					pojo.setG(getValue(row.getCell(6)));
					pojo.setH(getValue(row.getCell(7)));
					pojo.setI(getValue(row.getCell(8)));
					pojo.setJ(getValue(row.getCell(9)));
					pojo.setK(getValue(row.getCell(10)));
					list.add(pojo);
				}
			}

		}
		return list;
	}

	public List<Pojo> readXls(String path) throws IOException {
		System.out.println(Common.PROCESSING + path);

		InputStream input = new FileInputStream(path);
		HSSFWorkbook work = new HSSFWorkbook(input);
		Pojo pojo = null;
		List<Pojo> list = new ArrayList<Pojo>();
		for (int i = 0; i < work.getNumberOfSheets(); i++) {
			HSSFSheet sheet = work.getSheetAt(i);
			if (sheet == null) {
				continue;
			}
			for (int j = 0; j < sheet.getLastRowNum()-1; j++) {
				HSSFRow row = sheet.getRow(j);
				if (row != null) {
					pojo = new Pojo();
					pojo.setA(getValue(row.getCell(0)));
					pojo.setB(getValue(row.getCell(1)));
					pojo.setC(getValue(row.getCell(2)));
					pojo.setD(getValue(row.getCell(3)));
					pojo.setE(getValue(row.getCell(4)));
					pojo.setF(getValue(row.getCell(5)));
					pojo.setG(getValue(row.getCell(6)));
					pojo.setH(getValue(row.getCell(7)));
					pojo.setI(getValue(row.getCell(8)));
					pojo.setJ(getValue(row.getCell(9)));
					pojo.setK(getValue(row.getCell(10)));
					list.add(pojo);
				}
			}

		}
		return list;
	}

	private String getValue(HSSFCell hssfCell) {
		if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
			return String.valueOf(hssfCell.getBooleanCellValue());
		} else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
			return String.valueOf(hssfCell.getNumericCellValue());
		} else {
			return String.valueOf(hssfCell.getStringCellValue());
		}
	}

	private String getValue(XSSFCell xssfRow) {
		if (xssfRow.getCellType() == xssfRow.CELL_TYPE_BOOLEAN) {
			return String.valueOf(xssfRow.getBooleanCellValue());
		} else if (xssfRow.getCellType() == xssfRow.CELL_TYPE_NUMERIC) {
			return String.valueOf(xssfRow.getNumericCellValue());
		} else {
			return String.valueOf(xssfRow.getStringCellValue());
		}
	}
}
